home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / wart.arc / WART.DOC < prev    next >
Encoding:
Text File  |  1985-06-27  |  4.0 KB  |  113 lines

  1.  
  2. /*
  3.  
  4. WART
  5.  
  6. Wart is a program that implements a small subset of the Unix 'lex'
  7. lexical analyzer generator.  Unlike lex, wart may be distributed without
  8. requirement for a Unix license.  Wart was written at the Columbia University
  9. Center of Computing Activities to facilitate development of Unix Kermit.
  10.  
  11. Wart is intended for production of state table switchers.  It allows a
  12. set of states to be defined, along with a function for getting input.  The
  13. performs actions and switches states based on the current state and the
  14. input.
  15.  
  16. The following short program demonstrates some of the capabilities and
  17. limitations of Wart.  The program accepts from the command line a binary
  18. number, preceded by an optional minus sign, and optionally containing a
  19. fractional part.  It prints the decimal equivalent.
  20.  
  21. #include <stdio.h>
  22.  
  23. int state, s = 1, m = 0, d;
  24. float f;
  25. char *b;
  26.  
  27. %states sign mantissa fraction            /* Declare wart states */
  28.  
  29. %%                        /* Begin state table */
  30. <sign>-      { s = -1; BEGIN mantissa; }    /* Look for sign */
  31. <sign>0      { m = 0;  BEGIN mantissa; }    /* Got digit, start mantissa */
  32. <sign>1      { m = 1;  BEGIN mantissa; }
  33. <sign>.      { fatal("bad input "); }       /* Detect bad format */
  34. <mantissa>0  { m *= 2; }            /* Accumulate mantissa */
  35. <mantissa>1  { m = 2 * m + 1; }
  36. <mantissa>$  { printf("%d\n", s * m); return; }
  37. <mantissa>.  { f = 0.0; d = 1; BEGIN fraction; }    /* Start fraction */
  38. <fraction>0  { d *= 2; }                /* Accumulate fraction */
  39. <fraction>1  { d *= 2; f += 1.0 / d; }
  40. <fraction>$  { printf("%f\n", s * (m + f) ); return; }
  41. <fraction>.  { fatal("bad input"); }
  42. %%
  43.  
  44. input() {                    /* Define input() function */
  45.     int x;
  46.     return(((x = *b++) == '\0') ? '$' : x );
  47. }
  48.  
  49. fatal(s) char *s; {                /* Error exit */
  50.     fprintf(stderr,"fatal - %s\n",s);
  51.     exit(1);
  52. }
  53.  
  54. main(argc,argv) int argc; char **argv; {    /* Main program */
  55.     if (argc < 1) exit(1);
  56.     b = *++argv;
  57.     state = sign;                /* Initialize state */
  58.     wart();                    /* Invoke state switcher */
  59.     exit(0);                    /* Done */
  60. }
  61.  
  62. The wart program accepts as input a C program containing lines that start
  63. with "%" or sections delimited by "%%".  The directive "%states" declares
  64. the program's states.  The section enclosed by "%%" markers is the state
  65. table, with entries of the form
  66.  
  67.   <state>X { action }
  68.  
  69. which read as "if in state <state> with input X perform { action }"
  70.  
  71. The optional <state> field tells the current state or state the program must
  72. be in to perform the indicated action.    If no state is specified, then it
  73. means the action will be performed regardless of the current state.  If more
  74. than one state is specifed, then the action will be performed in any of the
  75. listed states.    Multiple states are separated by commas.
  76.  
  77. The required input field consists of a single literal character.  When in
  78. the indicated state, if the input is the specified character, then the
  79. associated action will be performed.  The character '.' matches any input
  80. character.  No pattern matching or range notation is provided.    The input
  81. character is obtained from the input() function, which you must define.  It
  82. should be alphanumeric, or else one of the characters ".% -$@" (quotes not
  83. included).  Note that the program above recognize the binary point '.'
  84. through a ruse.
  85.  
  86. The action is a series of zero or more C language statements, enclosed in
  87. curly braces.
  88.  
  89. The BEGIN macro is defined simply to be "state = ", as in lex.
  90.  
  91. The wart() function is generated by the wart program based on the state
  92. declarations and the state transition table.  It loops through calls to
  93. input(), using the result to index into a big case statement it has created
  94. from the state table.
  95.  
  96. Wart is invoked as follows:
  97.  
  98.     wart
  99.  
  100. Input from stdin, output to stdout
  101.  
  102.     wart fn1
  103.  
  104. Input from fn1, output to stdout
  105.  
  106.     wart fn1 fn2
  107.  
  108. Input from fn1, output to fn2.    Example:  wart a.w a.c
  109.  
  110. Wart programs have the conventional filetype '.w'.
  111. (18 Feb 85)
  112. */
  113.